<?php
    #-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	Kumomado::load('Kui.Components.Base.KumomadoUIBase');
	/**
	 * @package libraries/kui/components/base/
	 * @author Tsai, Min Che
	 * @version 1.0
	 */
	class KumomadoSelect extends KumomadoUIBase {
		protected $ui_name = 'KSelect';
		protected $options = array();
		protected $selected = null;
		protected $multiple = false;
		public function __construct($id, $options = array(), $multiple = false) {
			$this->setId($id);
			$this->addOptions($options);
			$this->setMultiple($multiple);
		}
		public function addOption($value, $text) {
			$this->options[] = array(
				'value' => $value,
				'text' => $text
			);
		}
		public function addOptions($options) {
			if (!is_array($options)) {
				return false;
			}
			foreach ($options as $option) {
				if (isset($option['value']) && isset($option['text'])) {
					$this->addOption($option['value'], $option['text']);
				}
			}
		}
		public function setSelected($value) {
			$this->selected = $value;
		}
		public function setMultiple($multiple = false) {
			$this->multiple = (boolean)$multiple;
		}
		public function getAttrs() {
			return array(
				'id' => $this->getId(),
				'options' => $this->options,
				'multiple' => $this->multiple
			);
		}
		public function view() {
			$view = '<select id="'.$this->getId().'" name="'.$this->getId().'" '.(true == $this->multiple ? 'multiple' : '').'>';
			foreach ($this->options as $option) {
				$view .= '<option value="'.$option['value'].'" '.($this->selected == $option['value'] ? 'selected="true"' : '').'>'.$option['text'].'</option>';
			}
			$view .= '</select>';
			return $view;
		}
		public function render() {
			return array(
				'ui_name' => $this->ui_name,
				'attrs' => $this->getAttrs(),
				'view' => $this->view()
			);
		}
	}
?>